<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my">我的</el-menu-item>
      <el-menu-item index="department" v-if="departmentSaleProfitReport">部门</el-menu-item>
      <el-menu-item index="all" v-if="allSaleProfitReport">全部</el-menu-item>
    </el-menu>
    <div style="margin-top: 20px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="130px" :rules="rules">
        <el-form-item label="订单编号：">
          <el-input v-model="listQuery.orderSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="订单日期：">
          <el-date-picker
                  v-model="listQuery.createTime"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户编号：">
          <el-input v-model="listQuery.customerNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户名称：">
          <el-select
            clearable
            v-model="listQuery.customerId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in customerList"
              :key="item.customerId"
              :label="item.customerName"
              :value="item.customerId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品名：">
          <el-select v-model="listQuery.goodsName" @change="changeGoodsName" clearable>
            <el-option v-for="item in goodsClassList" :value="item" :label="item" :key="item"></el-option>
          </el-select>
<!--          <el-input v-model="listQuery.goodsName"></el-input>-->
        </el-form-item>
        <el-form-item label="牌号：">
          <el-select v-model="listQuery.kingGoodsId" filterable placeholder="请选择" clearable>
            <el-option
                    v-for="item in goodsList"
                    :key="item.kingId"
                    :label="item.goodsSn"
                    :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售员：">
          <el-select v-model="listQuery.chargeUserId" placeholder="请选择" clearable filterable>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.realName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事业部：" v-if="listQuery.tab == 'all' || listQuery.tab=='department'">
          <el-select v-model="listQuery.divsionDeptId" filterable placeholder="请选择" clearable>
            <el-option :value="item.divsionDeptId" :label="item.divsionDeptName" v-for="item in divisionList" :key="item.divsionDeptId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="财务状态：">
          <el-select v-model.number="listQuery.financeState" class="input-width" style="width:195px" placeholder="请选择" clearable>
            <el-option :value="0" label="未审核" :key="0"></el-option>
            <el-option :value="1" label="已审核" :key="1"></el-option>
            <el-option :value="2" label="已关闭" :key="2"></el-option>
            <el-option :value="3" label="部分审核" :key="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付状态：">
          <el-select v-model.number="listQuery.payState" class="input-width" style="width:195px" placeholder="请选择" clearable>
            <el-option :value="0" label="未支付" :key="0"></el-option>
            <el-option :value="1" label="未结清" :key="1"></el-option>
            <el-option :value="2" label="已结清" :key="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开票状态：">
          <el-select v-model.number="listQuery.invoiceState" class="input-width" style="width:195px" placeholder="请选择" clearable>
            <el-option :value="0" label="未开票" :key="0"></el-option>
<!--            <el-option :value="1" label="部分开票" :key="1"></el-option>-->
            <el-option :value="2" label="已开票" :key="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="订单状态：">
          <el-select v-model="listQuery.orderState" class="input-width" style="width:195px" placeholder="请选择" clearable multiple>
            <el-option :value="item.orderState" :label="item.text" :key="item.orderState" v-for="item in orderStateList"></el-option>
          </el-select>

        </el-form-item>

        <el-form-item label="支付方式：">
          <el-select v-model="listQuery.transactionType" placeholder="请选择" clearable>
            <el-option key="BANK_PAY" label="银行汇款" value="BANK_PAY"></el-option>
            <el-option key="COD_PAY" label="货到付款" value="COD_PAY"></el-option>
            <el-option key="DEPOSIT_PAY" label="定金" value="DEPOSIT_PAY"></el-option>
            <el-option key="XISHANG_PAY" label="锡商支付" value="XISHANG_PAY"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否上传收货确认函：" label-width="140px">
          <el-select v-model="listQuery.receivedState" placeholder="请选择" clearable>
            <el-option key="1" label="是" value="1"></el-option>
            <el-option key="0" label="否" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="预计发货时间：">
          <el-date-picker
            v-model="listQuery.aboutSendDate"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="距离预计发货日期：">
          <el-select v-model="listQuery.intervalDays" placeholder="请选择" clearable>
            <el-option key="1" label="差1天" value="1"></el-option>
            <el-option key="2" label="差2天" value="2"></el-option>
            <el-option key="3" label="差3天" value="3"></el-option>
            <el-option key="4" label="差3天以上" value="4"></el-option>
            <el-option key="-1" label="超过1天" value="-1"></el-option>
            <el-option key="-2" label="超过2天" value="-2"></el-option>
            <el-option key="-3" label="超过3天" value="-3"></el-option>
            <el-option key="-4" label="超过3天以上" value="-4"></el-option>
          </el-select>

        </el-form-item>

        <el-form-item>
          <el-button
                  @click="handleSearchList()"
                  type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column
                prop="createTime"
                label="订单日期"
                align="center"
                width="100">
        </el-table-column>
        <el-table-column
                prop="orderSn"
                label="订单编码"
                align="center"
                width="150">
          <template slot-scope="scope">
            <el-link @click="toDetail('see',scope.row)" type="primary" style="font-size:12px;vertical-align: middle">
              {{scope.row.orderSn}}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
                label="客户"
                align="center"
                width="180">
          <template slot-scope="scope">
            <div>
              {{scope.row.customerNumber}}
            </div>
            <!--是否有权限-->
            <div v-if="scope.row.isShowCustomerName">{{scope.row.customerName}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="financeState"
          label="财务状态"
          width="120"
          align="center">
        </el-table-column>
        <el-table-column
          prop="payState"
          label="支付状态"
          width="120"
          align="center">
        </el-table-column>
        <el-table-column
          prop="invoiceState"
          label="开票状态"
          width="120"
          align="center">
        </el-table-column>
        <el-table-column
          prop="transactionType"
          label="支付方式"
          width="120"
          align="center">
        </el-table-column>

        <el-table-column
                class-name="cspantd"
                label="品名"
                align="center"
                width="180">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.goodsName }}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="牌号"
          align="center"
          width="180">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.goodsSn }}</div>
          </template>
        </el-table-column>
        <el-table-column
                prop="userName"
                label="销售员"
                align="center"
                width="100">
        </el-table-column>
        <el-table-column
                prop="ordGoodsNum"
                label="订单数量"
                align="center"
                width="100">
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="商品价格"
          align="center"
          width="150">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.goodsPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          align="center"
          label="已排货数量"
          width="120">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.goodsPlanNum }}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="已发货数量"
          align="center"
          width="120">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.goodsSendNum }}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="系统待发货数量"
          align="center"
          width="120">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.unSendNum }}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="cspantd"
          label="实际待发货数量"
          align="center"
          width="120">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.actualUnSendNum }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="invoiceGoodsNum"
          label="已开票数量"
          align="center"
          width="120">
        </el-table-column>
        <el-table-column
          prop="unInvoiceGoodsNum"
          label="未开票数量"
          align="center"
          width="120">
        </el-table-column>
        <el-table-column
                prop="orderState"
                label="合同流转状态"
                width="120"
                align="center">
        </el-table-column>

        <el-table-column
          prop="hasUploadContract"
          label="合同回传状态"
          width="120"
          align="center">
        </el-table-column>
        <el-table-column
          prop="needReceivedCount"
          label="收货需上传数量"
          width="120"
          align="center">
        </el-table-column>
        <el-table-column
          prop="hasReceivedCount"
          label="收货已上传数量"
          width="120"
          align="center">
        </el-table-column>

        <el-table-column
          class-name="cspantd"
          label="预计发货时间"
          width="120"
          align="center">
          <template slot-scope="scope">
            <div class="tdDiv" v-for="(item,index) in scope.row.ordGoodsList" :key="index" :style="{'border-bottom': index==scope.row.ordGoodsList.length-1 ? '0px solid #EBEEF5' : '1px solid #EBEEF5'}">{{item.aboutSendDate }}</div>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

  </div>
</template>

<script>
    import { saleReportList,orderGoodsList,getCustomerOption,getUserOption,getOrderStatList,getGoodsClass} from '@/api/partner'
    import { divisionDeptList } from '@/api/company';
    import {saleOrderReport} from '@/api/order'
    import {formatDate} from '@/utils/date';
    import store from '@/store'
    import { mapGetters } from 'vuex'
    const defaultListQuery={
      "tab" : "my", //我的：my，部门:department,所有：all
      "chargeUserId" : '',  //业务员ID
      "divsionDeptId" : '',//部门ID
      "kingGoodsId" : '', //商品ID
      "goodsName" : "",//品名
      "customerId" : '',//客户ID
      "customerNumber" : "",//客户编号
      "createTime" : [],//下单时间
      "financeState" : '', //财务状态（0-未审核，1已审核，2已关闭）
      "payState" : "",//支付状态（0未支付，1未结清，2已结清）
      "invoiceState" : "",//开票状态（0未开票，1部分开票，2已开票）
      orderState:[],
      transactionType:'',
      receivedState:'',
      aboutSendDate:[],
      intervalDays:'',
      orderSn:'',
      "pageNum" : 1,
      "pageSize" : 10
    };
    export default {
        name: "saleReport",
        data(){
            return{
                roles:store.getters.roles ? store.getters.roles : [],
                listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
                list:[],
                count:0,
                loading:false,
                goodsList:[],
                customerList:[],
                userOptionList:[],
                divisionList:[],
              orderStateList:[],
              rules:{

              },
              goodsClassList:[]
            }
        },
        computed: {
          ...mapGetters([
            'name',
            'mobile',
            'userId'
          ]),
          departmentSaleProfitReport() {
            if (this.roles.indexOf("departmentSaleProfitReport") != -1) {
              return true
            } else {
              return false
            }
          },
          allSaleProfitReport() {
            if (this.roles.indexOf("allSaleProfitReport") != -1) {
              return true
            } else {
              return false
            }
          },

        },
        created() {
          let now = new Date();
          let nowMonth = now.getMonth(); //当前月
          let nowYear = now.getFullYear(); //当前年
          //本月的开始时间
          let monthStartDate = new Date(nowYear, nowMonth, 1);
          //本月的结束时间
          let monthEndDate = new Date(nowYear, nowMonth+1, 0,23,59,59);
          let startDate = formatDate(monthStartDate, 'yyyy-MM-dd');
          let endDate = formatDate(monthEndDate, 'yyyy-MM-dd');
          this.listQuery.createTime = [startDate,endDate];
          this.getListData();
          this.getUserOptionList()
          this.getDivisionDeptList()
          this.getOrderStatListData()
          this.getGoodsClassList()
        },
        filters: {
            formatTime(time) {
                let date = new Date(time);
                return formatDate(date, 'yyyy-MM-dd')
            },
        },
        methods:{
          changeGoodsName(){
            this.goodsList = [];
            this.listQuery.kingGoodsId = ''
            this.getgoodsList()
          },
          getGoodsClassList(){
            getGoodsClass().then((res)=>{
              this.goodsClassList = res
            })
          },
          getgoodsList(){
            orderGoodsList({goodsName:this.listQuery.goodsName}).then((res)=>{
              this.goodsList = res.list
            })
          },

          //去详情页
          toDetail(type,item){
            let routeData = this.$router.resolve({name:'saleDetail',query:{id:item.orderId,type:type}});
            window.open(routeData.href);
            // this.$router.push({name:'saleDetail',query:{id:item.orderId,type:type}})
          },

          //获取订单状态列表
          getOrderStatListData(){
            getOrderStatList().then((res)=>{
              this.orderStateList = res;
            })
          },

          /**
           * 切换选择状态
           */
          handleSelect(key, keyPath) {
            this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
            this.listQuery.tab = key;
            let now = new Date();
            let nowMonth = now.getMonth(); //当前月
            let nowYear = now.getFullYear(); //当前年
            //本月的开始时间
            let monthStartDate = new Date(nowYear, nowMonth, 1);
            //本月的结束时间
            let monthEndDate = new Date(nowYear, nowMonth+1, 0,23,59,59);
            let startDate = formatDate(monthStartDate, 'yyyy-MM-dd');
            let endDate = formatDate(monthEndDate, 'yyyy-MM-dd');
            this.listQuery.createTime = [startDate,endDate];
            this.handleSearchList();
          },


          //获取事业部列表
          getDivisionDeptList(){
            divisionDeptList().then((res)=>{
              this.divisionList = res
            })
          },

          //获取员工下拉选项
          getUserOptionList(){
            getUserOption().then((res)=>{
              this.userOptionList = res;
            })
          },
          //查询获取客户列表
          remoteMethod(query) {
            if(query && query!= ''){
              this.loading = true;
              getCustomerOption({keywords:query}).then(response => {
                this.customerList = response;
                this.loading = false;
              }).catch(()=>{
                this.loading = false;
              });
            }
          },
            handleSearchList(){
                this.listQuery.pageNum = 1;
                this.getListData();
            },

            handleSizeChange(val){
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = val;
                this.getListData();
            },
            handleCurrentChange(val){
                this.listQuery.pageNum = val;
                this.getListData();
            },

            getListData(){
              this.loading = true;
              saleOrderReport(this.listQuery).then((res)=>{
                this.list = res.list;
                this.count = res.count;
                this.loading = false;
              }).catch(()=>{
                this.loading = false;
              })
            },

        },
    }
</script>

<style scoped lang="scss">
.cspantd{
  padding:0 !important;
  .cell{
    padding:0 !important;
    .tdDiv{
      padding:5px 10px;
      min-height: 20px;
      line-height: 20px;
      box-sizing: content-box;
      text-align: center;
    }
  }
}
</style>
